﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using log4net.Core;

namespace AppFx.BizTalk.Diagnostics.Log4net
{
    [Serializable]
    public class CustomLoggingEvent
    {
        public const string MessageNamespace = "urn://AppFx.Log4net.Appenders";
        public const string MessageRootElement = "CustomLoggingEvent";

        public string MessageType
        {
            get { return string.Format("{0}#{1}", MessageNamespace, MessageRootElement); }
        }
        
        public CustomLoggingEvent()
        {
            Properties = new Dictionary<string, string>();
        }

        public CustomLoggingEvent(LoggingEvent log4NetEvent)
        {
            Properties = new Dictionary<string, string>();

            MachineName = Environment.MachineName;
            Domain = log4NetEvent.Domain;
            Identity = log4NetEvent.Identity;
            Level = log4NetEvent.Level.DisplayName;
            LoggerName = log4NetEvent.LoggerName;
            RenderedMessage = log4NetEvent.RenderedMessage;
            ThreadName = log4NetEvent.ThreadName;
            TimeStamp = log4NetEvent.TimeStamp;
            UserName = log4NetEvent.UserName;
            UtcTimeStamp = DateTime.UtcNow;
            
            if(log4NetEvent.LocationInformation != null)
            {
                ClassName = log4NetEvent.LocationInformation.ClassName;
                FileName = log4NetEvent.LocationInformation.FileName;
                FullInfo = log4NetEvent.LocationInformation.FullInfo;
                LineNumber = log4NetEvent.LocationInformation.LineNumber;
                MethodName = log4NetEvent.LocationInformation.MethodName;                
            }

            var properties = log4NetEvent.GetProperties();
            if(properties != null)
            {
                var keys = properties.GetKeys();
                foreach(var key in keys)
                {
                    var propertyValue = log4NetEvent.LookupProperty(key);
                    Properties.Add(key, propertyValue.ToString());
                }
            }
            ExceptionString = log4NetEvent.GetExceptionString();
            if(log4NetEvent.ExceptionObject != null)
            {
                ExceptionMessage = log4NetEvent.ExceptionObject.Message;                
            }
        }
        
        public Dictionary<string, string> Properties { get; set; }

        public string CorrelationId { get; set; }
        public string EventType { get; set; }
        public string MachineName { get; set; }
        public string ExceptionMessage { get; set; }
        public string ThreadName { get; set; }
        public DateTime UtcTimeStamp { get; set; }
        
        public DateTime TimeStamp { get; set; }
        public string UserName { get; set; }
        public string RenderedMessage { get; set; }
        public string Domain { get; set; }
        public string ExceptionString { get; set; }
        public string Identity { get; set; }
        public string Level { get; set; }
        public string ClassName { get; set; }
        public string FileName { get; set; }
        public string FullInfo { get; set; }
        public string LineNumber { get; set; }
        public string MethodName { get; set; }
        public string LoggerName { get; set; }
        public string ApplicationName { get; set; }        
    }
}


